package val.to.common;

import android.graphics.Bitmap;
import android.util.Log;
import val.to.BitmapUtil;
import val.to.config.Constants;

public class ImageFetcher implements Runnable {

	public interface ImageFetcherListener
	{
		void onImageFetchCompleted(Bitmap bmp);
	}
	
	private ImageFetcherListener mListener;
	private String mImageUri;
	private ITaskCanceler mCanceler;
	
	public ImageFetcher(String imageUri, ImageFetcherListener listener, ITaskCanceler canceler){
		mListener = listener;
		mImageUri = imageUri;
	}
	

	@Override
	public void run() {
		try
		{
			Bitmap bmp =  BitmapUtil.getBitmap(mImageUri);
			if (bmp != null && (mCanceler == null || !mCanceler.isCanceled())){
				mListener.onImageFetchCompleted(bmp);
			}
		}
		catch(Exception ex)
		{
			Log.e(Constants.TAG, String.format("Failed to fetch image: %s", ex.getMessage()));
		}	
	}
}
